package com.workspace.js_website.service;

import com.workspace.js_website.mapper.LogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 日志记录相关服务
 */

@Service
public class LogService {

    private final LogMapper logMapper;
    private final List<String> productList;

    @Autowired
    public LogService(LogMapper logMapper) {
        this.logMapper = logMapper;
        File file = new File("D:/lsn/jszx_resource/CoreProduct/");
        String[] products = file.list();
        productList = new ArrayList<>();
        if (products != null)
            for (String pro : products)
                productList.add(pro.substring(0, pro.indexOf(".")));
        else
            productList.add("暂无数据");
    }

    public void addLog(String userId, String behavior, String ip) {
        try {
            logMapper.addLog(userId, behavior, ip);
        } catch (Exception e) {
            System.out.println("记录失败，报错信息为:" + e);
        }
    }

    public boolean checkIp(String behavior, String ip) {
        int count = logMapper.checkIp(behavior, ip, "1/24");
        return count == 0;
    }

    public Map<String, Object> getLogCount(String start, String end) {
        int register = logMapper.getRegistrationCount(start, end);
        int login = logMapper.getLogCount("login", start, end);
        int browse = logMapper.getLogCount("browseWebsite", start, end);
        List<Map<String, Object>> product = new ArrayList<>();
        for (String pro : productList) {
            Map<String, Object> el = new HashMap<>();
            el.put("name", pro);
            el.put("times", logMapper.getLogCount("requestProduct-" + pro, start, end));
            product.add(el);
        }
        Map<String, Object> re = new HashMap<>();
        re.put("register", register);
        re.put("login", login);
        re.put("browse", browse);
        re.put("product", product);
        return re;
    }

    public List<Map<String, String>> getProductsByName(String name) {
        List<Map<String, String>> ori = logMapper.getProductsByName(name);
        List<Map<String, String>> re = new ArrayList<>();
        for (Map<String, String> m : ori) {
            Map<String, String> el = new HashMap<>();
            el.put("name", m.get("NAME"));
            el.put("product", m.get("BEHAVIOR").substring(m.get("BEHAVIOR").indexOf("-") + 1));
            el.put("date", m.get("TIME"));
            re.add(el);
        }
        return re;
    }

    public List<Map<String, String>> getUsersByProduct(String product) {
        List<Map<String, String>> ori = logMapper.getUsersByProduct(product);
        List<Map<String, String>> re = new ArrayList<>();
        for (Map<String, String> m : ori) {
            Map<String, String> el = new HashMap<>();
            el.put("name", m.get("NAME"));
            el.put("product", m.get("BEHAVIOR").substring(m.get("BEHAVIOR").indexOf("-") + 1));
            el.put("create_time", m.get("TIME"));
            el.put("mobile", m.get("MOBILE"));
            el.put("company", m.get("COMPANY"));
            el.put("email", m.get("EMAIL"));
            re.add(el);
        }
        return re;
    }
}
